/* linkit.js
 * By François Leporcq
 * MIT Licensed.
 */
var AbstractWorkspace = $Class.extend({
    _className: "AbstractWorkspace",
    getContainer: function(){
        return this._container;
    },
    getShapes: function(){
        return this._shapes;
    },
    draw : function(shape){
        throw new Error("The 'draw(AbstractShape shape)' method must be overrided in '" + this._className + "' class");
    },
    redraw: function(shape){
        this.clear(shape);
        this.draw(shape);
    },
    clearAll: function(shape){
        throw new Error("The 'clearAll()' method must be overrided in '" + this._className + "' class");
    },
    clear: function(shape){
        throw new Error("The 'clear(AbstractShape shape)' method must be overrided in '" + this._className + "' class");
    },
    add: function(shape){
        if(shape == null){
            return;
        }
        if(!(shape instanceof AbstractShape)){
            throw new Error("The object to add must be an instance of AbstractShape extended class");
        }
        this.getShapes().push(shape);
        shape.afterWorkspaceAdd(this);
    },
    remove: function(shape){
        if(shape){
            return null;
        }
        for(var i = 0; i < this.getShapes().length ; i++){
            if(shape === this.getShapes()[i]){
                this.getShapes().splice(i,1);
                delete shape["element"];
                return shape;
            }
        }
        return null;
    },
    removeAll: function(){
        for(var i = 0; i < this.getShapes().length ; i++){
            this.remove(this.getShapes()[i]);
        }
    },
    drawAll: function(){
        for(var i = 0 ; i < this.getShapes().length ; i++){
            this.draw(this.getShapes()[i]);
        }
    },
    reset: function(shape){
        this.clear(shape);
        delete shape.element;
        if(shape instanceof Composite){
            var subshapes = shape.getShapes();
            for(var i = 0 ; i < subshapes.length ; i++){
                this.reset(subshapes[i]);
            }
        }
    },
    _applyCssClasses: function(shape, cssClasses){
        if(!cssClasses){
            cssClasses = new Array();
        }
        if(!shape.isPartOfComposite() && shape.implementationOf(Linkable)){
            cssClasses.push("linkable");
            if(shape.hasLinks()){
                cssClasses.push("linked");
            }
        }

        if(shape instanceof AbstractLink){
            cssClasses.push("link");
        }
        if(shape instanceof Circle){
            cssClasses.push("circle");
        }
        if(shape instanceof Ellipse){
            cssClasses.push("circle");
        }
        if(shape instanceof Point){
            cssClasses.push("point");
        }
        if(shape instanceof Text){
            cssClasses.push("text");
        }
        if(shape instanceof Rectangle){
            cssClasses.push("rectangle");
        }
        if(shape instanceof Label){
            cssClasses.push("label");
        }
        if(shape instanceof Polyline){
            cssClasses.push("polyline");
        }
        if(shape instanceof Polygon){
            cssClasses.push("polygon");
        }
        if(shape instanceof Line){
            cssClasses.push("line");
        }
        if(shape instanceof BezierCurve){
            cssClasses.push("curve");
        }
        if(shape instanceof Polyline){
            cssClasses.push("polyline");
        }

        this._addCssClasses(shape, cssClasses);

    }
}, true);

//Static fields and methods
var Workspace = {
    label: {
        margin: 20,
        padding: 5
    },
    text: {
        anchor: "middle",
        fontSize: 14,
        fontFamily: "Arial"
    }

}
